package com.attendance.sz.net;

import android.content.Context;
import android.widget.Toast;

import java.net.ConnectException;

import retrofit2.adapter.rxjava.HttpException;
import rx.Subscriber;

/**
 * Created by Administrator on 2016-06-01.
 */
public abstract class AbsAPICallback<T> extends Subscriber<T> {
    Context mContext;


    public AbsAPICallback(Context context) {
        mContext = context;
        /**
         * 进度条
         */

    }


    @Override
    public void onNext(T t) {

    }

    @Override
    public void onStart() {
        super.onStart();


    }

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {
        Throwable throwable = e;
        //获取最根源的异常
        while (throwable.getCause() != null) {
            e = throwable;
            throwable = throwable.getCause();

        }

        if (e instanceof ConnectException) {
            Toast.makeText(mContext, "网络异常", Toast.LENGTH_SHORT).show();
        } else if (e instanceof HttpException) {
            if (((HttpException) e).code() >= 500)
                Toast.makeText(mContext, "服务器异常", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
        }


    }


}
